package com.cssnj.portal.web.user;

import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.user.UserService;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Controller
@RequestMapping(value = "/user/statistics")
public class StatisticsController {

    @Autowired
    UserService userService;


    @RequestMapping(value = {"/saveGnDjs.do"})
    @ResponseBody
    public JsonResponse saveGnDjs(HttpServletRequest request) throws BaseException, SQLException {
        //记录本人功能菜单点击记录
        JsonRequest jsonRequest = new JsonRequest();
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        jsonRequest.setServiceName("userService");
        jsonRequest.setHandleCode("saveGnDjs");
        jsonRequest.getReqData().put("gnid", request.getParameter("gnid"));
        jsonRequest.getReqData().put("swrydm", userInfo.getSwryDm());
        jsonRequest.getReqData().put("swjgdm", userInfo.getSfswJgDm());
        jsonRequest.getReqData().put("swrysfdm", userInfo.getSwrySfDm());
        JsonResponse jsonResponse = userService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"/djscxInit.do"})
    public ModelAndView djscxInit(HttpServletRequest request) throws BaseException, SQLException {
        //页面初始化
        ModelAndView modelAndView = new ModelAndView("user/djscx");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String cxrqq = format.format(c.getTime());
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String cxrqz = format.format(ca.getTime());
        modelAndView.getModelMap().put("cxrqq", cxrqq);
        modelAndView.getModelMap().put("cxrqz", cxrqz);
        return modelAndView;
    }

    @RequestMapping(value = {"/queryGnDjLb.do"})
    @ResponseBody
    public String queryGnDjLb(HttpServletRequest request) throws BaseException, SQLException {
        //分页查询功能点击记录
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("userService");
        jsonRequest.setHandleCode("queryGnDjLb");
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        jsonRequest.getReqData().put("swrysfdm", userInfo.getSwrySfDm());
        if (null != request.getParameter("page")) {
            jsonRequest.getReqData().put("page", request.getParameter("page"));
        }
        if (null != request.getParameter("rows")) {
            jsonRequest.getReqData().put("rows", request.getParameter("rows"));
        }
        if (null != request.getParameter("cxrqq")) {
            jsonRequest.getReqData().put("cxrqq", request.getParameter("cxrqq"));
        }
        if (null != request.getParameter("cxrqz")) {
            jsonRequest.getReqData().put("cxrqz", request.getParameter("cxrqz"));
        }
        JsonResponse jsonResponse = userService.execute(jsonRequest);
        String str = (String) jsonResponse.getRepData().get("str");
        return str;
    }
}
